using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace PlayTime
{
    public partial class ContractBridgeCallDisplay : Form
    {
        private int nWidth = 30;
        private int nHeight = 20;

        private Hand m_cHnad;
        private CallStatus m_eCallStatus;
        public ContractBridgeCallDisplay(Hand hand)
        {
            m_cHnad = hand;
            InitializeComponent();
            m_eCallStatus = CallStatus.CallIMidway;
        }
        public void CallClickHandler(Object sender, System.EventArgs e)
        {
            if (m_eCallStatus == CallStatus.CallComplete)
            {
                return;
            }
            object tag = ((System.Windows.Forms.Button)sender).Tag;
            string text = ((System.Windows.Forms.Button)sender).Text;
            string strCall = (string)tag;
            Position pos = m_cHnad.CurrentCaller;
            Call curCall = new Call(strCall, pos);
            string callStatus = "";

            CallStatus callStat = m_cHnad.ProcessCall(curCall, callStatus);

            switch (callStat)
            {
                case CallStatus.CallComplete:
                    UpdateCall(curCall, pos, text);
                    ShowWinner();
                    break;
                case CallStatus.BidPassedOut:
                    m_cHnad.PassedOut();
                    Close();
                    break;
                case CallStatus.CallIMidway:
                    UpdateCall(curCall, pos, text);
                    ShowNextCaller();
                    break;
                case CallStatus.InvalidCall:
                    break;
            }
            m_eCallStatus = callStat;
        }









        private void btnBidDone_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void ContractBridgeCallDisplay_Load(object sender, EventArgs e)
        {
            ShowNextCaller();
            ShowCallTable();
            ShowCallMadeTable();
        }

        private void ShowNextCaller()
        {
            lblBidder.Text = m_cHnad.CurrentCaller.ToString() + " to give  call";
        }
        private void ShowWinner()
        {
            lblBidder.Text = "Declarer: " + m_cHnad.Declarer.ToString() + " contract: " + m_cHnad.TopCall.BidLevel + m_cHnad.TopCall.BidDenomination + " Lead " + m_cHnad.OpeningLead;
        }

        private void ShowCallMadeTable()
        {
            PopulateEmptyCallTable();
        }

        private void ShowCallTable()
        {
            int X, Y;
            int X0 = 20;
            int Y0 = 20;
            X = X0;
            Y = Y0;

            int nHorizontalDistance = 5;
            int nVerticalDistance = 5;

            int dx = nWidth + nHorizontalDistance;
            int dy = nHeight + nVerticalDistance;


            for (BidDenomination bd = BidDenomination.C; bd <= BidDenomination.NT; bd++) //row
            {
                X = X0;
                for (int nBidQuantity = 1; nBidQuantity <= 7; nBidQuantity++)
                {
                    ShowCallButton(X, Y, CallType.Bid, bd, nBidQuantity);
                    X += dx;
                }
                Y += dy;
            }
            X = X0;


            for (CallType ct = CallType.Pass; ct <= CallType.ReDouble; ct++)
            {
                ShowCallButton(X, Y, ct);
                Y += dy;
            }
        }

        private void ShowCallButton(int X, int Y, CallType ct, BidDenomination bd, int nBidQuantity)
        {
            string text = Call.GetCallButtonText(ct, bd, nBidQuantity);
            string tag = Call.GetCallButtonTag(ct, bd, nBidQuantity);
            ShowButton(X, Y, text, tag);
        }
        private void ShowCallButton(int X, int Y, CallType ct)
        {
            string text = Call.GetCallButtonText(ct);
            string tag = Call.GetCallButtonTag(ct);
            ShowButton(X, Y, text, tag);
        }

        private void ShowButton(int X, int Y, string text, string tag)
        {
            System.Windows.Forms.Button aButton = new System.Windows.Forms.Button();

            aButton.Click += new System.EventHandler(CallClickHandler);
            aButton.Left = X;
            aButton.Top = Y;
            aButton.Font = new System.Drawing.Font("Lucida Sans Unicode", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

            aButton.Height = nHeight;
            aButton.Width = nWidth;

            aButton.Text = text;
            aButton.Tag = tag;
            aButton.AutoSize = true;

            this.Controls.Add(aButton);
            this.groupBid.Controls.Add(aButton);
        }
        


        private void UpdateCall(Call curCall, Position pos,string text)
        {
           

            System.Windows.Forms.Control[] aButton = null;
            ArrayList playerCallList = m_cHnad.GetCallListByPlayer(pos);
            int n = 0;
            n = playerCallList.Count - 1;
            //if (pos < m_cHnad.Session.Dealer.Position)
            //{
            //    n++;
            //}            

            string nameControl = pos.ToString();// +"_" + n.ToString();
            switch (pos)
            {
                case Position.North:
                    aButton = this.groupNorth.Controls.Find(nameControl, true);
                    break;
                case Position.East:
                    aButton = this.groupEast.Controls.Find(nameControl, true);
                    break;
                case Position.South:
                    aButton = this.groupSouth.Controls.Find(nameControl, true);
                    break;
                case Position.West:
                    aButton = this.groupWest.Controls.Find(nameControl, true);
                    break;
            }
            if (aButton.Length >= 1)
            {
                int nButtontoChange = -1;
                for ( int i = 0; i < aButton.Length; i++)
                {
                    if (aButton[i].Visible == false)
                    {
                        nButtontoChange = i;
                        break;
                    }
                }
                if (nButtontoChange >= 0)
                {
                    aButton[nButtontoChange].Text = text;
                    aButton[nButtontoChange].Visible = true;
                }
            }
        }
      
        private void PopulateEmptyCallTable()
        {
            int X;
            int Y;
            int X0 = 10;
            int Y0 = 20;
            X = X0;
            Y = Y0;
            
            int nHorizontalDistance = 5;
            int nVerticalDistance = 5;

            int dx = nWidth + nHorizontalDistance;
            int dy = nHeight + nVerticalDistance;

            for (Position pos = Position.North; pos <= Position.West; pos++)
            {
                Y = Y0;
                
                for (int i = 0; i < 8; i++, Y += dy)
                {
                    string name ;
                    string callTag = pos.ToString();
                    name = callTag;
                    string callText = "-" ;
                    bool bShow = false;
                    if ((i == 0) && (pos < m_cHnad.Session.Dealer.Position))
                    {
                        name = "-99";
                        bShow = true;
                        callText = "-";
                    }
                   
                    ShowBidMade(X, Y, pos, callTag, callText,name, bShow);
                }
                X += dx;
            }
        }

        private void ShowBidMade(int X, int Y, Position pos,string callTag, string callText,string name, bool bShow)
        {
        
            System.Windows.Forms.Button aButton = new System.Windows.Forms.Button();
            
            aButton.Font = new System.Drawing.Font("Lucida Sans Unicode", 8.25F, System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,((System.Byte)(0)));

            aButton.Height = nHeight;
            aButton.Width = nWidth;
            aButton.Name = name; 
            aButton.Text = callText;
            aButton.Tag = callTag;
            aButton.AutoSize = true;
            aButton.Visible = bShow; 
            aButton.Left = 5;
            aButton.Top = Y;
            
            switch (pos)
            {
                case Position.North:
                    this.groupNorth.Controls.Add(aButton);
                    break;
                case Position.East:
                    this.groupEast.Controls.Add(aButton);
                    break;
                case Position.South:
                    this.groupSouth.Controls.Add(aButton);
                    break;
                case Position.West:
                    this.groupWest.Controls.Add(aButton);
                    break;
            }
        }
    }
}